/* ----------------------------------------------------------------------
 *	Shadow Quest
 *	Copyright (C) 2009 Prageeth Silva <prageeth@thenewcoders.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  ----------------------------------------------------------------------
 */

package sq.game;


import map.World;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import sq.ImageLoader;
import sq.MouseAnimation;

public class GameCursor
{

	public enum CursorState 
	{ 
		NONE, NORMAL, INVALID, MOVE, ATTACK, TALK, ACTIVATE
	}
	
	private int x, y;
	private MouseAnimation display;
	private Quest quest;
	private CursorState state;
	
	
	public GameCursor(Quest quest) 
	throws SlickException
	{
		
		super();
		this.setQuest(quest);
		this.setState(CursorState.NONE);
		
	}

	public void update(Input input, int delta)
    throws SlickException
    {
		
		boolean mouse = input.isMousePressed(Input.MOUSE_RIGHT_BUTTON);
		
		this.moveTo(input.getMouseX(), input.getMouseY());
		
		// TODO testing
		if (mouse)
		{
			// TODO Quest.getCurrentPlayer().setTargetToWalk(this.getWorldX(), this.getWorldY());
		}
		
		// TODO change states
		this.setState(CursorState.NORMAL);
		
		this.getDisplayCursor().update(delta);
		
	}
	
	public void render(GameContainer gc, Graphics g)
    throws SlickException
    {
		g.drawAnimation(this.getDisplayCursor().getDisplayAnimation(),
				this.getX()-this.getDisplayCursor().getHotspotX(),
				this.getY()-this.getDisplayCursor().getHotspotY());
    }

	public MouseAnimation getDisplayCursor() 
	{
		return this.display;
	}

	private void setDisplayCursor(MouseAnimation display)
	{
		this.display = display;
	}

	private World getWorld()
	{
		return this.getQuest().getWorld();
	}
	
	private Quest getQuest()
	{
		return this.quest;
	}

	private void setQuest(Quest quest)
	{
		this.quest = quest;
	}

	protected CursorState getState()
	{
		return this.state;
	}

	private void setState(CursorState state)
	{
		this.state = state;
		this.setDisplayCursor(ImageLoader.getGameCursor(state)); 
	}

	private void moveTo(int x, int y)
	{
		this.setX(x);
		this.setY(y);
	}
	
	private int getX() 
	{
		return this.x;
	}

	private void setX(int x) 
	{
		this.x = x;
	}

	private int getY()
	{
		return this.y;
	}

	private void setY(int y)
	{
		this.y = y;
	}
	
	private float getWorldX() 
	{
		return (this.getWorld().getCameraX() + this.getX());
	}

	private float getWorldY()
	{
		return (this.getWorld().getCameraY() + this.getY());
	}
		
}
